package com.gears_software.bbnetwork.activitys;

import java.util.List;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.WindowFeature;
import org.androidannotations.annotations.sharedpreferences.Pref;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.avos.avoscloud.AVAnalytics;
import com.avos.avoscloud.LogUtil.log;
import com.gears_software.bbnetwork.R;
import com.gears_software.bbnetwork.adapters.YinghangAdapter;
import com.gears_software.bbnetwork.datas.finders.IBBNetworkFinder;
import com.gears_software.bbnetwork.datas.finders.impl.IBBNetworkFinderImpl;
import com.gears_software.bbnetwork.datas.models.AchieveBankIconUrls;
import com.gears_software.bbnetwork.datas.models.ResultModel;
import com.gears_software.bbnetwork.datas.models.ReturnCode;
import com.gears_software.bbnetwork.utlis.BBNetworkSharedPref_;
import com.gears_software.bbnetwork.utlis.CommonUtils;
import com.gears_software.bbnetwork.utlis.ToastUI;

/**
 * 绑定银行卡页面
 * 
 * @author as
 * 
 */

@EActivity(R.layout.activity_card)
@WindowFeature(Window.FEATURE_NO_TITLE)
public class PinlessCardActivity extends Activity {
	
	PopupWindow popupWindow;
	int windowHeight;
	
	@ViewById
	TextView text_select;// 选择银行
	
	@ViewById
	EditText edit_kh;//开户行
	
	@ViewById
	EditText edit_number;//卡号
	

	@Bean(IBBNetworkFinderImpl.class)
	IBBNetworkFinder finder;

	@Pref
	BBNetworkSharedPref_ sp;

	@Bean
	ToastUI toast;

	List<AchieveBankIconUrls> list;
	InputMethodManager imm;

	String cashName = null;

	@AfterViews
	void onCreate() {
		CommonUtils.showDialog(this);
		imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		getlistIcon();
		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		// width = dm.widthPixels;// 宽度height = dm.heightPixels ;//高度
		windowHeight = dm.heightPixels;
	}
	
	//保存
	@Click
	void button_ok(){
		String depositBankName = edit_kh.getText().toString();//开户行
		String cardNo = edit_number.getText().toString();//开户行
		if(cardNo==null||cardNo.equals("")){
			showTxt("卡号不可为空！");
			return;
		}
		if(depositBankName==null||depositBankName.equals("")){
			showTxt("开户行不可为空！");
			return;
		}
		if(cashName==null){
			showTxt("请选择银行！");
			return;
		}
		
		if(cardNo.length()<12){
			showTxt("银行卡号不可小于12位");
			return;
		}
		
		CommonUtils.showDialog(this);
		bindCard(cardNo, depositBankName);
	}
	
	@Background
	void bindCard(String cardNo,String depositBankName){
		ResultModel<String> res = finder.getBindCard(sp.userId().get(), cardNo, cashName, depositBankName);
		if(res.getReturnCode().equals(ReturnCode.SUCCEED)){
			showTxt("绑定成功！");
			sp.hasBankCard().put("1");
			finish();
		}else{
			showTxt(res.getMessage());
		}
		closeDialog();
	}

	// 选择银行
	@Click
	void text_select() {
		closeInput();
		if (list == null || list.size() == 0) {
			showTxt("没有银行数据");
			return;
		}
		
		try {
			popupWindow.showAtLocation(new View(this), Gravity.BOTTOM, 0, 0);
		} catch (Exception e) {
		}
	}

	// 关闭软键盘
	void closeInput() {
		if (getCurrentFocus() != null) {
			((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE))
					.hideSoftInputFromWindow(
							getCurrentFocus().getWindowToken(),
							InputMethodManager.HIDE_NOT_ALWAYS);
		}
	}

	// 初始化弹出窗口
	@UiThread
	void initWindow() {
		View view = getLayoutInflater().from(this).inflate(
				R.layout.list_window, null);
		ListView listview = (ListView) view.findViewById(R.id.list_window);
		YinghangAdapter adapter = new YinghangAdapter(list, this);
		listview.setAdapter(adapter);
		listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				log.d("tag", "sdfsdf");
				AchieveBankIconUrls model = list.get(arg2);
				if (model != null) {
					text_select.setText(model.getBankName());
					cashName = model.getBankName();
					popupWindow.dismiss();
				}
			}
		});
		popupWindow = new PopupWindow(view, LayoutParams.MATCH_PARENT, 400);
		popupWindow.setAnimationStyle(android.R.style.Animation_Toast);
		popupWindow.setBackgroundDrawable(new ColorDrawable(0));
		popupWindow.setOutsideTouchable(true);
		popupWindow.setFocusable(true);
	}

	@Background
	void getlistIcon() {
		ResultModel<List<AchieveBankIconUrls>> res = finder.getYinhangList(sp
				.userId().get());
		if (res.getReturnCode().equals(ReturnCode.SUCCEED)) {
			list = res.getResObj();
			initWindow();
		} else {
			showTxt(res.getMessage());
		}
		closeDialog();
	}

	@Click
	void image_mycard_goback() {
		finish();
	}

	@UiThread
	void showTxt(String txt) {
		toast.ShowToast(txt);
	}

	@UiThread
	void closeDialog() {
		CommonUtils.closeDialog();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		AVAnalytics.onResume(this);
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		AVAnalytics.onPause(this);
	}

}
